package tech.aistar.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

/**
 * @Author: lya
 * @Description: 本类的作用描述:该servlet负责展示登录界面
 *                 视图形式的servlet - 充当的是View视图层
 * @CreateDate: 2019/9/4
 * @Version: 1.0
 */

@WebServlet("/user/loginView")
public class LoginViewAction extends HttpServlet{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.service(req, resp);
        //设置文档类型
        resp.setContentType("text/html;charset=utf-8");

        //获取文档输出流
        PrintWriter out = resp.getWriter();

        //接受登陆处理的servlet中的错误标识error
        String error = req.getParameter("error");

        //=================
        //获取cookie
        //每次请求的时候,会将所有的cookie全部发送给server
        Cookie[] cookies = req.getCookies();

        String uname="";
        String pwd="";
        //定义一个标记位
        boolean flag = false;

        if(null!=cookies && cookies.length>0){
            for(Cookie c:cookies){
                //找到name=username的cookie
                if("userLogin".equals(c.getName())){
                    String value = c.getValue();

                    if(value.trim().length()==0){
                        break;
                    }
                    // System.out.println(value.split(":")[0]);

                    //如果对用户名进行二级编码,那么此处需要进行二级解码
                    uname = value.split(":")[0];

                    uname = URLDecoder.decode(URLDecoder.decode(uname,"utf-8"),"utf-8");

                    pwd = value.split(":")[1];

                    flag = true;
                    break;
                }
            }
        }

        //====================

        out.println("<!DOCTYPE html>");
        out.println("<html lang='en'>");

        out.println("<head>");
        out.println("<meta charset='UTF-8'>");
        out.println("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
        out.println("<meta http-equiv='X-UA-Compatible' content='ie=edge'>");
        out.println("<title>登录注册页面</title>");
        out.println("<link rel='stylesheet' href='"+req.getContextPath()+"/plugins/bootstrap/css/bootstrap.css' type='text/css'>");

        out.println("</head>");

        out.println("<body>");
        out.println("   <div class='container'>");
        out.println("       <div class='row'>");
        out.println("           <div class='modal-content'>");
        out.println("               <div class='modal-header'>");
        out.println("                   <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times</button>");
        out.println("                   <h4 class='modal-title' id='myModalLabel'>用户登录入口 </h4>");
        out.println("               </div>");
        out.println("               <div class='modal-body'>");
        out.println("               <!--表单登录-->");
                                    //修改form表单的action和method
                                    // /user/login是专门接受数据的
        out.println("               <form action='"+req.getContextPath()+"/user/login' method='post' class='form-horizontal' role='form'>");
        out.println("                   <div class='form-group has-feedback'>");
        out.println("                       <label for='firstname' class='col-sm-2 control-label'>用户名 </label>");
        out.println("                       <div class='col-md-6'>");
        out.println("                           <input type='text' value='"+uname+"' name='username' class='form-control' id='firstname' placeholder='请输入用户名'>");
        out.println("                           <span class='form-control-feedback glyphicon glyphicon-user'> </span>");
        out.println("                       </div>");
        if ("0".equals(error)){
            //提示用户名不存在
            out.println("<div class='col-md-3' style='padding-top:3px'>");
            out.println("   <span class='text-danger'>用户不存在!</span>");
            out.println("</div>");
        }

        if ("2".equals(error)){
            out.println("<div class='col-md-3' style='padding-top:3px'>");
            out.println("   <span class='text-danger'>请您先登录</span>");
            out.println("</div>");
        }
        out.println("                   </div>");
        out.println("                   <div class='form-group has-feedback'>");
        out.println("                       <label for='lastname' class='col-sm-2 control-label'>密码 </label>");
        out.println("                       <div class='col-md-6'>");
        out.println("                           <input type='password'  value='"+pwd+"' name='password' class='form-control' id='lastname' placeholder='请输入密码'>");
        out.println("                           <span class='form-control-feedback glyphicon glyphicon-lock'> </span>");
        out.println("                       </div>");

        //密码错误提示!
        if("1".equals(error)) {
            out.println("<div class='col-md-3' style='padding-top:3px'>");
            out.println("   <span class='text-danger'>密码错误!</span>");
            out.println("</div>");
        }

        out.println("                   </div>");
        out.println("                   <div class='form-group'>");
        out.println("                       <div class='col-sm-offset-2 col-sm-10'>");
        out.println("                           <div class='checkbox'>");
        out.println("                               <label>");

        //        如果记住了密码,下次就自动勾选
        if(flag) {
            out.println("<input type='checkbox' checked name='flag'>请记住我</label>");
        }else{
            out.println("<input type='checkbox' name='flag'>请记住我</label>");
        }
        out.println("                           </div>");
        out.println("                       </div>");
        out.println("                   </div>");
        out.println("                   <div class='form-group'>");
        out.println("                       <div class='col-sm-offset-2 col-sm-10'>");
        out.println("                           <button type='submit' class='btn btn-default'>登录</button>");
        out.println("                       </div>");
        out.println("                   </div>");
        out.println("               </form>");
        out.println("           </div>");
        out.println("       </div>");
        out.println("   </div>");
        out.println("   <script src='"+req.getContextPath()+"/plugins/jquery/jquery.min.js'> </script>");
        out.println("   <script src='"+req.getContextPath()+"/plugins/bootstrap/js/bootstrap.js'> </script>");
        out.println("</body>");

        out.println("</html>");
    }
}
